/*
 * Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
 *
 * The software is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

package com.lv.common.widget.picker;

import androidx.annotation.NonNull;

import com.github.gzuliyujiang.wheelpicker.contract.LinkageProvider;
import com.lv.common.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者： ygx
 * 创建日期：2023/4/11 9:48 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：二级联动数据
 */
public class ThirdTimeLikeProvider implements LinkageProvider {
    private List<String> mTimes = new ArrayList<>();
    private List<String> mMouths = new ArrayList<>();
    private List<String> mDays = new ArrayList<>();
    private boolean mIsHasAll = false;

    public ThirdTimeLikeProvider(List<String> times, boolean hasAll) {

        mTimes.clear();
        mTimes.addAll(times);
        mIsHasAll = hasAll;
//        if (mIsHasAll) {
//            mTimes.add(0, "全部");
//        }
    }

    @Override
    public boolean firstLevelVisible() {
        return true;
    }

    @Override
    public boolean thirdLevelVisible() {
        return true;
    }

    @NonNull
    @Override
    public List<String> provideFirstData() {
        return mTimes;
    }

    @NonNull
    @Override
    public List<String> linkageSecondData(int firstIndex) {
        mMouths.clear();
        String s = mTimes.get(firstIndex);
        if (s.equals("全部")) {
            mMouths.add("全部");
        } else {
            if (mIsHasAll) {
                mMouths.add("全部");
            }
            int k = 13;
            if (s.equals(TimeUtil.getYear() + "年")) {
                k = TimeUtil.getMonth() + 1;
            } else {
                k = 13;
            }
            for (int i = 1; i < k; i++) {
                String mouth = String.format("%02d", i);
                mMouths.add(mouth + "月");
            }
        }
        return mMouths;
    }

    @NonNull
    @Override
    public List<String> linkageThirdData(int firstIndex, int secondIndex) {
        mDays.clear();
        String mouth = mMouths.get(secondIndex);
        if (mouth.equals("全部")) {
            mDays.add("全部");
        } else {
            if (mIsHasAll) {
                mDays.add("全部");
            }
            String year = mTimes.get(firstIndex);
            int daysByDate = TimeUtil.getDaysByDate(year + "-" + mouth);
            int month = TimeUtil.getMonth();
            String monthCure = String.format("%02d", month);

            if (year.equals(TimeUtil.getYear() + "年") && mouth.equals(monthCure + "月")) {
                daysByDate = TimeUtil.getDay() + 1;
            } else {
                daysByDate = TimeUtil.getDaysByDate(year + "-" + mouth);
            }
            for (int i = 1; i < daysByDate; i++) {
                String day = String.format("%02d", i);
                mDays.add(day + "日");
            }
        }
        return mDays;
    }

    @Override
    public int findFirstIndex(Object firstValue) {
        return (int) firstValue;
    }

    @Override
    public int findSecondIndex(int firstIndex, Object secondValue) {
        return (int) secondValue;
    }

    @Override
    public int findThirdIndex(int firstIndex, int secondIndex, Object thirdValue) {
        return (int) thirdValue;
    }

}
